from datetime import timedelta
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from src.config.settings import settings
from celery import Celery

db = SQLAlchemy()
migrate = Migrate()


def creat_celery_app():
    celery_app = Celery('tasks', broker=settings.CELERY_BROKER_URL, backend=settings.CELERY_RESULT_BACKEND)
    # app.extensions["celery"] = celery_app
    
    imports = [
        "src.schedule.health_beat",
        # "src.tasks.export_tasks",
        # "src.tasks.split_pdf_task",
    ]
    day = 1
    seconds = 5
    beat_schedule = {
        "health_beat": {
            "task": "src.schedule.health_beat.health_beat",
            "schedule": timedelta(seconds=seconds),
        },
    }
    celery_app.conf.update(beat_schedule=beat_schedule, imports=imports)
    
    # 自动发现 src.tasks 包下所有任务
    celery_app.autodiscover_tasks(['src.tasks', 'src.schedule'])
    
    
    # 设置默认的队列
    celery_app.set_default() 
    
    return celery_app


celery_app = creat_celery_app()
